// pages/remindlogin/index.js
const app = getApp()
const service = require('../../service/port.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo')
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },
  getUserInfo: function (e) {
    console.log(e);
    if (e.detail.userInfo) {
      app.globalData.userInfo = e.detail
      wx.checkSession({
        success: function (res) {
          service.login({
            code: wx.getStorageSync('code').code,
            encryptedData: e.detail.encryptedData,
            iv: e.detail.iv,
            id:wx.getStorageSync('qrcodeId') || 0
          }, function (result) {
            console.log(result);
            wx.setStorageSync("userInfo", result.data);
            service.getdiamondNum(result.data.id, res => { //获取钻石数量
              //console.log(res)
              wx.setStorageSync("diamondNum", res.data.scort);
              wx.navigateBack({
                delta: 1
              })
    
            })
          })
        },
        fail: function (res) {
          console.log("需要重新登录");
          wx.login({
            success: (res1) => {
              wx.setStorageSync("code", res1);
              service.login({
                code: res1.code,
                encryptedData: e.detail.encryptedData,
                iv: e.detail.iv,
                id:wx.getStorageSync('qrcodeId') || 0
                
              }, function (result) {
                console.log(result);
                wx.setStorageSync("userInfo", result.data);
                service.getdiamondNum(result.data.id, res => { //获取钻石数量
                  //console.log(res)
                  wx.setStorageSync("diamondNum", res.data.scort);
                  wx.navigateBack({
                    delta: 1
                  })
        
                })
              })
            }
          })
        }
      })
      
      this.setData({
        userInfo: e.detail.userInfo,
        hasUserInfo: true
      })

    }

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})